home *** CD-ROM | disk | FTP | other *** search
/ Windows Expert / Windows Expert.iso / others / ole_101.zip / SCHMOO.ZIP / OLEINIT.C < prev    next >
C/C++ Source or Header  |  1992-04-13  |  6KB  |  208 lines

  1. /*
  2.  * OLEINIT.C
  3.  *
  4.  * Handles all application and instance-specific initialization that
  5.  * is also specific to OLE.  That which is specific only to the
  6.  * application or instance resides in INIT.C
  7.  *
  8.  * Copyright(c) Microsoft Corp. 1992 All Rights Reserved
  9.  *
  10.  */
  11.  
  12. #ifdef MAKEOLESERVER
  13.  
  14. #include <windows.h>
  15. #include <ole.h>
  16. #include "schmoo.h"
  17. #include "oleglobl.h"
  18.  
  19.  
  20.  
  21.  
  22. /*
  23.  * FOLEInstanceInit
  24.  *
  25.  * Purpose:
  26.  *  Handles OLE-server specific initialziation:
  27.  *      1.  Register clipboard formats for "Native", "OwnerLink",
  28.  *          and "ObjectLink."
  29.  *      2.  Initialize VTBLs for server, document, and object.
  30.  *      3.  Register the server with OLESVR.
  31.  *      4.  Parse the command line to determine initial window state.
  32.  *      5.  Register a document depending on contents of command line.
  33.  *
  34.  * Parameters:
  35.  *  pOLE            LPXOLEGLOBALS to OLE-specific global variable block.
  36.  *  hInstance       HANDLE of the application instance.
  37.  *  pszClass        LPSTR to classname of the server.
  38.  *  ppszCmds        LPSTR FAR to command line argument strings.
  39.  *  nCmdShow        WORD initial ShowWindow command passed to WinMain.
  40.  *
  41.  * Return Value:
  42.  *  BOOL            FALSE if an error occurred, otherwise TRUE and
  43.  *                  pOLE->wCmdShow contains a valid ShowWindow command
  44.  *                  for the initial state of the window.
  45.  */
  46.  
  47. BOOL FAR PASCAL FOLEInstanceInit(LPXOLEGLOBALS pOLE,
  48.                                  HANDLE hInstance, LPSTR pszClass,
  49.                                  LPSTR FAR *ppszCmds, WORD nCmdShow)
  50.     {
  51.     OLESTATUS       os;
  52.     LPSTR           pszT;
  53.     LPSCHMOOSERVER  pSvr;
  54.     LPSCHMOODOC     pDoc;
  55.     BOOL            fTemp;
  56.  
  57.  
  58.     //1. Register clipboard formats.
  59.     pOLE->cfNative    =RegisterClipboardFormat(rgpsz[IDS_NATIVE]);
  60.     pOLE->cfOwnerLink =RegisterClipboardFormat(rgpsz[IDS_OWNERLINK]);
  61.     pOLE->cfObjectLink=RegisterClipboardFormat(rgpsz[IDS_OBJECTLINK]);
  62.  
  63.     if (0==pOLE->cfNative || 0==pOLE->cfOwnerLink || 0==pOLE->cfObjectLink)
  64.         return FALSE;
  65.  
  66.  
  67.     /*
  68.      * 2. Initialize the method tables with functions in OLEVTBL.C
  69.      *    and mark the vtbl's as initialized.
  70.      */
  71.     fTemp=TRUE;
  72.     fTemp&=FOLEVtblInitServer  (hInstance, &pOLE->vtblSvr);
  73.     fTemp&=FOLEVtblInitDocument(hInstance, &pOLE->vtblDoc);
  74.     fTemp&=FOLEVtblInitObject  (hInstance, &pOLE->vtblObj);
  75.  
  76.     if (!fTemp)
  77.         return FALSE;   //Cleanup will follow through FApplicationExit.
  78.  
  79.  
  80.     /*
  81.      * 3. Allocate and initialize the OLESERVER structure, in this
  82.      *    application we call it SCHMOOSERVER.  Uses the server constructor
  83.      *    in OLESVR.C.
  84.      */
  85.  
  86.     pSvr=PServerAllocate(&pOLE->vtblSvr);
  87.  
  88.     if ((LPSCHMOOSERVER)NULL==pSvr)
  89.         return FALSE;
  90.  
  91.     pOLE->pSvr=pSvr;
  92.     pSvr->wCmdShow=nCmdShow;        //By default, use what the app was given.
  93.  
  94.  
  95.     //4. Register the server application with OLESVR.DLL
  96.     os=OleRegisterServer(pszClass, (LPOLESERVER)pSvr, &pSvr->lh,
  97.                          hInstance, OLE_SERVER_MULTI);
  98.  
  99.     if (OLE_OK!=os)
  100.         {
  101.         //ServerRelease will NOT be called so we must free the memory.
  102.         LocalFree(pSvr->hMem);
  103.         return FALSE;
  104.         }
  105.  
  106.  
  107.  
  108.     /*
  109.      * 5. Given the pointer to the command-line strings, ppszCmds,
  110.      *    check if we have "[/ | -]Embedding" and a possible filename.
  111.      *
  112.      * NOTE:  We trust whoever called us to find a filename in
  113.      *        the command line, ignoring anything that starts with
  114.      *        a - or / as an argument.
  115.      */
  116.  
  117.     pszT=*ppszCmds++;
  118.  
  119.     //Assume stand-alone configuration.
  120.     pGlob->fOLE=FALSE;
  121.     pSvr->fEmbed=FALSE;
  122.     pSvr->fLink=FALSE;
  123.  
  124.     if (NULL!=pszT)
  125.         {
  126.         /*
  127.          * If the first parameter has - or / leading, skip that character
  128.          * so we can check for "Embedding"
  129.          */
  130.         if('-'==*pszT || '/'==*pszT)
  131.             pszT++;
  132.  
  133.         //See if we have Embedding at all, in which case we're doing OLE.
  134.         pGlob->fOLE=!lstrcmp(pszT, rgpsz[IDS_EMBEDDING]);
  135.  
  136.         //Change the ShowWindow command appropriately if we're doing OLE.
  137.         if (pGlob->fOLE)
  138.             pSvr->wCmdShow=SW_HIDE;
  139.  
  140.  
  141.         /*
  142.          * Set fLink if there is an additional filename on the
  143.          * command-line, and point pszT there if so.
  144.          */
  145.         if (NULL!=*ppszCmds)
  146.             {
  147.             pSvr->fLink=TRUE;
  148.             pszT=*ppszCmds;
  149.             }
  150.         else
  151.             {
  152.             pSvr->fEmbed=TRUE;
  153.             pszT=NULL;
  154.             }
  155.  
  156.         //pszT is either NULL or points to a filename.
  157.         }
  158.  
  159.     /*
  160.      * 6.  Allocate an OLESERVERDOC (SCHMOODOC) and initialize.  LPTR
  161.      *     to LocalAlloc initializes everything to NULL.
  162.      */
  163.     pDoc=PDocumentAllocate(&pOLE->vtblDoc);
  164.  
  165.     if ((LPSCHMOODOC)NULL==pDoc)
  166.         {
  167.         //DocRelease will not be called, but ServerRelease will.
  168.         LocalFree(pDoc->hMem);
  169.         OleRevokeServer(pSvr->lh);
  170.         return FALSE;
  171.         }
  172.  
  173.     pSvr->pDoc=pDoc;
  174.  
  175.  
  176.     /*
  177.      * 7.  Register documents as necessary.  Remember to register any
  178.      *     stand-alone or linked document regardless of what you are
  179.      *     doing with OLE.  The only case where you do NOT register is
  180.      *     for starting the application as embedded.
  181.      *
  182.      *     In step 5 we set pszT equal to either NULL (embedding or no
  183.      *     file) or to a filename (linking or stand-alone with a file).
  184.      *     So we only have to point pszT to "(Untitled)" if it's NULL
  185.      *     then call OleRegisterServerDoc.
  186.      */
  187.  
  188.     //Register a document in any case except embedding.
  189.     if (!pSvr->fEmbed)
  190.         {
  191.         pszT=(NULL==pszT) ? rgpsz[IDS_UNTITLED] : pszT;
  192.         os=OleRegisterServerDoc(pSvr->lh, pszT, (LPOLESERVERDOC)pDoc, &pDoc->lh);
  193.  
  194.         //Must revoke on any error.
  195.         if (OLE_OK!=os)
  196.             {
  197.             OleRevokeServer(pSvr->lh);
  198.             return FALSE;
  199.             }
  200.         }
  201.  
  202.     //All done!
  203.     return TRUE;
  204.     }
  205.  
  206.  
  207. #endif //MAKEOLESERVER
  208.